package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityInfo;
import com.yuan.model_community.repository.CommunityInfoDao;
import com.yuan.model_community.service.CommunityInfoService;
import com.yuan.model_community.service.dto.CommunityInfoDto;
import com.yuan.model_community.service.mapstruct.CommunityInfoMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityInfoServiceImpl implements CommunityInfoService {

    private final CommunityInfoDao communityInfoDao;

    private final CommunityInfoMapper communityInfoMapper;

    @Override
    public PageResult<CommunityInfoDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityInfo> page = communityInfoDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(communityInfoMapper::toDto));
    }

    @Override
    public List<CommunityInfoDto> queryAll() {
        List<CommunityInfo> all = communityInfoDao.findAll();
        return communityInfoMapper.toDto(all);
    }

    @Override
    public List<CommunityInfoDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityInfo> users = communityInfoDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityInfoMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityInfoDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityInfoDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("小区Id", communityInfoDto.getId());
            map.put("小区详情", communityInfoDto.getCommInfo());
            map.put("创建时间", communityInfoDto.getBuildNum());
            map.put("小区地址", communityInfoDto.getCommAddress());
            map.put("楼盘数量", communityInfoDto.getBuildNum());
            map.put("小区名称", communityInfoDto.getCommName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityInfo resources) {
//        if (communityInfoDao.findCommunityNoticeByUserId(resources.getUserId()) != null) {
//            throw new EntityExistException(CommunityNotice.class, "userId", String.valueOf(resources.getUserId()));
//        }
//        if (communityInfoDao.findCommunityNoticeByFullName(resources.getFullName()) != null) {
//            throw new EntityExistException(CommunityNotice.class, "fullName", resources.getFullName());
//        }
        communityInfoDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Integer id, CommunityInfo resources) throws Exception {
        Optional<CommunityInfo> byId = communityInfoDao.findById(id);
        if (byId.isPresent()) {
            BeanUtils.copyProperties(resources, byId.get());
            byId.get().setId(id);
            communityInfoDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityInfoDao.deleteById(ids);
    }
}
